package com.enterprise.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.assg2.dao.*;
import com.movie.Owner;
import com.movie.Viewer;
import javax.servlet.http.HttpSession;

/**
 * This is the command that will be used for logging in users.
 * If logon is successful, the command should place a list of contacts
 * in the request attriubutes.
 * @author  yunki
 */
public class ViewerPage implements Command {

    /** Creates a new instance of LoginCommand */
    public ViewerPage() {
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream
        
                return "/myprofile.jsp";
//		response.setContentType("text/html");// from response, set content type
//		PrintWriter out = response.getWriter();// from response, get output writer
//		out.println("<b>	Welcome to viewers page!</b>");
	    //return null;

//        response.setContentType("text/html");// from response, set content type
//        PrintWriter out = response.getWriter();// from response, get output writer
//
//        HttpSession session = request.getSession(true);
//
//        Object user = (Object) session.getAttribute("user");
//
//        if ((user != null) && (user instanceof Viewer)) {
//            response.sendRedirect("dispatcher?operation=PAGE_NOT_FOUND");
//        }
//        if ((user != null) && (user instanceof Owner)) {
//            response.sendRedirect("dispatcher?operation=PAGE_NOT_FOUND");
//        }
//
//        return "//header.html").include(request, response);
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        user = User(username, password);
//        if (username == null || password == null) {
//            out.write("<div>");
//            out.write("<h3>Welcome to <b>Cinema World,</b> <span> Please Login</span></h3>");
//            out.write("<form method='POST' action='dispatcher?operation=login'>");
//            out.write("<p>Username: <span> <input type='text' name='username' size='15' /><br/>");
//            out.write("<p>Password: <span> <input type='password' name='password' size='15' /><br/></p>");
//            out.write("<p><input type='submit' value='Login' class='button'/></p>");
//            out.write("</div>");
//            out.write("</form>");
//            return "//footer.html").include(request, response);
//        } else if (user == null) {
//            out.println("<BODY>Your login and password are invalid.<BR>");
//            out.println("You may want to <A HREF='dispatcher?operation=login'>try again</A>");
//            out.println("</BODY></HTML>");
//            return "//footer.html").include(request, response);
//        } else {
//            session.setAttribute("user", user);
//            out.println("<BODY>Access Granted.<BR>");
//            out.println("You may want to <A HREF='dispatcher?operation=login'>try again</A>");
//            out.println("</BODY></HTML>");
//            return "//footer.html").include(request, response);
//        }
//        return null;
    }

    protected Object UserOwner(String user, String password) {
        DaoOwner daoowner = new DaoOwner();
        Owner owner = daoowner.findByUsername(user);

        DaoViewer daoviewer = new DaoViewer();
        Viewer viewer = (Viewer) daoviewer.findBy("username", user);

        if (owner != null) {
            if (owner.getPassword().matches(password)) {
                return owner; // trust everyone
            } else if (viewer != null) {
                if (viewer.getPassword().matches(password)) {
                    return viewer;
                } else {
                    return null;
                }

            } else {
                return null;
            }
        }
        return null;
    }

    protected Object User(String user, String password) {
        DaoOwner daoowner = new DaoOwner();
        Owner owner = daoowner.findByUsername(user);

        DaoViewer daoviewer = new DaoViewer();
        Viewer viewer = (Viewer) daoviewer.findBy("username", user);

        if (owner != null) {
            if (owner.getPassword().matches(password)) {
                return owner; // trust everyone
            } else if (viewer != null) {
                if (viewer.getPassword().matches(password)) {
                    return viewer;
                } else {
                    return null;
                }

            } else {
                return null;
            }
        }
        return null;
    }
}
